package com.umfb.timertask;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.wall.WallConfig;
import com.alibaba.druid.wall.WallFilter;

import com.umfb.timertask.config.mybatis.DruidDataProperties;
import org.springframework.beans.BeanUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


@Configuration
@EnableConfigurationProperties(DruidDataProperties.class)
public class DatabaseConfig {

	// private DruidDataProperties druidDataProperties;


	@Bean
	public DataSource druidDataSource(DruidDataProperties properties) {
		DruidDataSource druidDataSource = new DruidDataSource();
		BeanUtils.copyProperties(properties, druidDataSource);

		// druidDataSource.set
		// properties.getDriverClassName()
		// properties.getData();

		// druidDataSource.setDriverClassName(driver);
		// druidDataSource.setUrl(url);
		// druidDataSource.setUsername(username);
		// druidDataSource.setPassword(password);
		// druidDataSource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
		// druidDataSource.setUseGlobalDataSourceStat(useGlobalDataSourceStat);

		// druidDataSource.configFromPropety(properties);
		// configFromPropety
		// try {
		// druidDataSource.setFilters("stat, wall");
		// } catch (SQLException e) {
		// e.printStackTrace();


		return druidDataSource;
	}

	//自己配置filer从而可以配置批量执行sql语句
	@Bean
	public WallFilter wallFilter(){
		WallFilter wallFilter = new WallFilter();
		wallFilter.setConfig(wallConfig());
		return wallFilter;
	}
	@Bean
	public WallConfig wallConfig(){
		WallConfig wallConfig = new WallConfig();
		wallConfig.setMultiStatementAllow(true);//允许批量执行sql语句
		return wallConfig;
	}
	/**
	 * 这里是 druid 监控 默认关闭了
	 * 
	 * @return
	 */
	// @Bean
	// public ServletRegistrationBean druidServlet() {
	//
	// return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
	// }

}
